/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonyapp;

import com.matrimonybeans.LoginLocal;
import com.matrimonybeans.RequestLocal;
import com.matrimonybeans.ViewCandidateLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ChauAnh
 */
public class Login extends HttpServlet {

    @EJB
    private ViewCandidateLocal viewCandidate;
    @EJB
    private RequestLocal sendRequest;
    @EJB
    private LoginLocal login;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String user = request.getParameter("txtUsername");
            String pass = request.getParameter("txtPassword");

            char userArr[] = user.toCharArray();
            String role = "";
            for (int i = 0; i < 5; i++) {
                role = role + userArr[i];
            }

            if (role.equalsIgnoreCase("admin")) {
                if (login.checkExistAdmin(user, pass)) {
                    HttpSession ss = request.getSession(true);
                    ss.setAttribute("login_admin", user);
                    ss.setAttribute("fullName_admin", login.getFullName());
                    ss.setAttribute("avatar_admin", login.getAvatar());
                    ss.setMaxInactiveInterval(1000);
                    response.sendRedirect("index_admin.jsp");
                } else {
                    out.println("<script type='text/javascript'>alert('Username or Password of Admintrator incorrect. Please login agian!');window.location.href = 'index.jsp';</script>");
                }
            } else {
                if (login.checkExists(user, pass)) {
                    String gender = viewCandidate.getGender(user);
                    String country = viewCandidate.getCountry(user);
                    String city = viewCandidate.getCity(user);
                    HashMap listRequest = sendRequest.getAllRequest(user);
                    out.println("City: " + city + " country:" + country + " gender:" + gender);
                    HashMap listRMCandidate = viewCandidate.RelatedMatchesCandidate(user,gender , country , city);                    
                    HttpSession ss = request.getSession(true);                   
                    ss.setAttribute("login", user);
                    ss.setAttribute("fullName", login.getFullName());
                    ss.setAttribute("avatar", login.getAvatar());
                    ss.setAttribute("totalRequest", sendRequest.countRequest(user));
                    ss.setAttribute("listRequest", listRequest);
                    ss.setAttribute("listRMCandidate", listRMCandidate);
                    ss.setMaxInactiveInterval(1000);
                    //Huy cac ss tim kiem cua khach (vi menber tim kiem co them dieu kien gender)
                    ss.removeAttribute("listFindCandidate");                    
                    ss.removeAttribute("searchValue");
                    
                    response.sendRedirect("index.jsp");
                } else {
                    out.println("<script type='text/javascript'>alert('Username or Password incorrect. Please login agian!');window.location.href = 'index.jsp';</script>");
                }
            }
        } finally {
            out.close();
        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
